Import diddy
Import globals
Import udts

'OHRRPGCE CUSTOM - Map editor and map picker
'(C) Copyright 1997-2005 James Paige and Hamster Republic Productions
'Monkey Port by Landon Ritchie
Class MapEditState

	Field mapnum:int
	Field editmode:Int
	Field seteditmode:Int
	Field x:int
	Field y:int
	Field mapx:int
	Field mapy:Int = -80
	Field layer:int
	Field cur_zone:Int
	Field tileoffset:Int
	Field cur_tile_id:int
	Field cur_zinfo:ZoneInfo ' = GetZoneInfo(zmap, st.cur_zone)	
end


Class MapEditorScreen Extends Screen
	Field menu:SimpleMenu
	Field musicFormat:String
	Field st:MapEditState = New MapEditState
	'dude said he wants 3, when where i never?
	Field zonetileset:Int[3]
	Field overlaytileset:Int
	Field TileSlider:SimpleSlider
	'
	
	#rem
	DIM modenames(5) as string
	DIM gmap(dimbinsize(binMAP)) as integer
	DIM pal16(288) as integer
	DIM npcnum(max_npc_defs - 1) as integer
	DIM her as HeroDef
	DIM hero_gfx as GraphicPair	
	REDIM doors(99) as door
	REDIM link(199) as doorlink	
	#end
	
	'Field backgroundImage:Image
	
	Method New(mapnum:int)
		name = "MAP"
		st.mapnum = mapnum
		st.editmode = 0
		st.seteditmode = -1	
		st.x = 0
		st.y = 0
		st.mapx = 0
		st.mapy = 0
		st.layer = 0
		st.cur_zone = 1			
		'backgroundImage = LoadImage(diddyGame.images.path + "WaterTemple.png")
	End
	
	Method Start:Void()

		menu = New SimpleMenu("ButtonOver", "ButtonClick", 0, 0, 10, False,HORIZONTAL)
		Local b:SimpleButton = menu.AddButton("drawtool.png", "drawtoolMO.png")
		b = menu.AddButton("boxtool.png", "boxtoolMO.png")
		b = menu.AddButton("filltool.png", "filltoolMO.png")
		b = menu.AddButton("replacetool.png", "replacetoolMO.png")
		b = menu.AddButton("importtool.png", "importtoolMO.png")		
		menu.SetY(32)
		menu.SetX(256)		
		'considering most of the original source was used for dos style editing we are going to overhaul all of it.
		TileSlider = New SimpleSlider("slider_bar.png", "slider.png", 16, 40, 35, "Scroll", 20, True)
		TileSlider.SetValue(0)


		'menu.Centre()
	End
	
	Method Render:Void()
		Cls
		SetAlpha(1)	
		SetColor(0,0,0)
		DrawRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)		
		'DrawImage backgroundImage, 0, 0
		SetColor(255,255,255)
		'DrawImage MainLogo,160,32
		SetAlpha(1)	
		'damn thing keeps culling half the map wtf
		Tilemaps[st.mapnum].RenderMap(st.mapx*32,(st.mapy*32)-80, SCREEN_WIDTH+(SCREEN_WIDTH/2), SCREEN_HEIGHT+(SCREEN_HEIGHT/2))
		SetColor(80,80,80)
		DrawRect(0,0,SCREEN_WIDTH,80)
		Local mx:Int = diddyGame.mouseX
		Local my:Int = diddyGame.mouseY
		If Not diddyGame.virtualResOn
			mx = MouseX()
			my = MouseY()
		End		
		'draw tilemap's tileset
		For Local ts:TileMapTileset = Eachin Tilemaps[st.mapnum].tilesets.Values()
			For Local i:Int = 0 To ts.tiles.Length-1
				SetColor(255,255,255)
				DrawImage(ts.image.image,16+(i*32)-(st.tileoffset*32),16,i)
				If st.cur_tile_id = i Then
					SetAlpha(0.5)
					SetColor(255,255,255)
					DrawRect(((i*32)-(st.tileoffset*32)),0,32,32)
					SetAlpha(1)					
				Endif
				If mx >= ((i*32)-(st.tileoffset*32)) And mx < (32+(i*32)-(st.tileoffset*32)) And my >= 0 And my < 32 Then
					SetAlpha(0.5)
					SetColor(255,255,0)
					DrawRect(((i*32)-(st.tileoffset*32)),0,32,32)	
					SetAlpha(1)
					SetColor(255,255,255)		
					If MouseDown( MOUSE_LEFT ) Then
						st.cur_tile_id = i	
					Endif	
				Endif
			Next
		Next
		

		If st.y > 80 Then	
			SetAlpha(0.5)
			SetColor(255,255,255)			
			DrawRect(st.x,st.y,32,32)
			SetAlpha(1)	
		Endif

		
	End
	
	Method ExtraRender:Void()
		menu.Draw()
		TileSlider.Draw()
	End
	
	Method Update:Void()
		menu.Update()

		Local mx:Int = diddyGame.mouseX
		Local my:Int = diddyGame.mouseY
		If Not diddyGame.virtualResOn
			mx = MouseX()
			my = MouseY()
		End	
		
		'hover map tile
		st.x = Int((mx/32)*32)'+(st.mapx*32)
		st.y = Int((my/32)*32)'+(st.mapy*32)
		
		If st.y > 80 Then	
			If MouseDown( MOUSE_LEFT ) Then
				'place tilemap tile
				Local newy:Int = my-80
				
				Local tileid:Int = Int((mx/32)+st.mapx)+(Int((newy/32)+st.mapy)*Tilemaps[st.mapnum].layers.Get(st.layer).width)
				If tileid > -1 And tileid < TileMapTileLayer(Tilemaps[st.mapnum].layers.Get(st.layer)).mapData.tiles.Length then
					Print "TILE ID IS: "+tileid+" Layer width "+Tilemaps[st.mapnum].layers.Get(st.layer).width
					TileMapTileLayer(Tilemaps[st.mapnum].layers.Get(st.layer)).mapData.tiles[tileid] = st.cur_tile_id+1
					TileMapTileLayer(Tilemaps[st.mapnum].layers.Get(st.layer)).mapData.cells[tileid].gid = st.cur_tile_id+1
				endif
			
			Endif
		endif		
		If KeyDown(KEY_UP) Then st.mapy -= 1
		If KeyDown(KEY_DOWN) Then st.mapy += 1
		If KeyDown(KEY_LEFT) Then st.mapx -= 1
		If KeyDown(KEY_RIGHT) Then st.mapx += 1				
		
		If menu.Clicked("maptiles") Then
			FadeToScreen(maptiles)
		End
		
		If TileSlider.Update() Then
			st.tileoffset = Int(160.00*Float(TileSlider.value*0.01))
		End If
		
		If KeyHit(KEY_ESCAPE) Or menu.Clicked("quit")
			FadeToScreen(mainedscreen)
		End
	End
End